﻿using CodeApi.Context;
using CodeApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CodeApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DashBoardController : ControllerBase
    {
        public DashBoardContext dashBoardContext;
        public DashBoardController(DashBoardContext dashBoardContext)
        {
            this.dashBoardContext = dashBoardContext;
        }

        [HttpGet("{uid}")]
        public async Task<ActionResult<DashBoardModel>> getUserDashBoard(int uid)
        {
            DashBoardModel dashBoardModel = new DashBoardModel();
            if (await dashBoardContext.articles.AnyAsync(t=>t.UID==uid))
            {
                IList<ArticleModel> list = await dashBoardContext.articles.Where(t => t.UID == uid).ToListAsync();
                dashBoardModel.SumArticle = list.Count.ToString();
                foreach (var item in dashBoardContext.articles)
                {
                    dashBoardModel.ViewNumbers += item.Number;
                }
                return dashBoardModel;
            }
            return dashBoardModel;
            
        }
    }
}
